{% extends "base.html" %}
{% load i18n %}
{% load technologies_tags %}
{% block title %} {{ object }} {% endblock %}

{% block center_area %}

<link rel="stylesheet" type="text/css" href="/site_media/xadmin/css/themes/bootstrap-xadmin.css" />
<link href="/site_media/xadmin/vendor/font-awesome/css/font-awesome.css" type="text/css" media="screen" rel="stylesheet" />
<link href="/site_media/xadmin/css/xadmin.main.css" type="text/css" media="screen" rel="stylesheet" />
<link href="/site_media/xadmin/css/xadmin.responsive.css" type="text/css" media="screen" rel="stylesheet" />
<link href="/site_media/xadmin/css/xadmin.form.css" type="text/css" media="screen" rel="stylesheet" />

<div class="container">
    <h1>{{ object }}</h1>
    <a class="btn btn-default active" href="{{redirecturl}}">{% trans '返回' %}</a>
    <a id="show_manufactureplan_modal" class="pull-right btn btn-primary active" data-toggle="modal" href="#ManufactureplanDetail">查看生产通知单</a>
    <div class="bs-yuankong">
        {% technology_workflow object %}
        {% if approve_permission %}
        <button id='btn_approve' type="button" data-toggle="modal" href="#modal_approve" class="btn btn-primary">{% trans 'Approve' %}</button>
        {% endif %}
        {% if refusal_permission %}
        <button id='btn_refusal' type="button" data-toggle="modal" href="#modal_refusal" class="btn btn-danger">{% trans 'Refusal' %}</button>
        {% endif %}
    </div>
</div>

<!-- Modal:ManufactureplanDetail -->
<div id="ManufactureplanDetail" class="modal fade detail-modal" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">{{ object }}</h4>
      </div>
      <div id="ManufactureplanDetailBody" class="modal-body">
      </div>
      <div class="modal-footer">
        <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">{% trans "Close" %}</button>
      </div>
    </div>
  </div>
</div>

<!-- Modal:ApproveModal -->
<div class="modal fade" id="modal_approve" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="modal_approve_label">{% trans "please fill the approve note" %}</h4>
            </div>
            <div class="modal-body">
                <textarea id="approve_note" name="note" class="form-control" rows="5"> </textarea>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" id="btn_refusal_cancel" name="cancel">{% trans "cancel" %}</button>
                <button type="button" class="btn btn-primary" id="btn_approve_ok" name="ok">{% trans "ok" %}</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal:RefusalModal -->
<div class="modal fade" id="modal_refusal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="modal_refusal_label">{% trans "please fill the refusal note" %}</h4>
            </div>
            <div class="modal-body">
                <textarea id="refusal_note" name="note" class="form-control" rows="5"> </textarea>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" id="btn_refusal_cancel" name="cancel">{% trans "cancel" %}</button>
                <button type="button" class="btn btn-primary" id="btn_refusal_ok" name="ok">{% trans "ok" %}</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal:WorkflowLogModal -->
<div class="modal fade" id="workflow_log" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">{% trans "this note is :" %}</h4>
      </div>
      <div class="modal-body">
        <h4 id='workflow_log_note'></h4>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
      </div>
    </div>
  </div>
</div>

<!-- Modal:WarningModal -->
<div class="modal fade" id="warning" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="warningLabel">{% trans "Warning :" %}</h4>
      </div>
      <div class="modal-body">
        <h4 id='warning_note'></h4>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
      </div>
    </div>
  </div>
</div>


<script type="text/javascript">

    $("#show_manufactureplan_modal").click(function(event) {
        $.ajax({
            type: "GET",
            url: "{% url admin:manufactureplan_manufactureplan_changelist %}"+"{{ object.id }}"+"/detail/?_format=html",
            success: function(data) {
              $("#ManufactureplanDetailBody").html(data);
            },
            error: function () {
                alert('fail');
            }
        });
    });

    $.ajaxSetup({ 
         beforeSend: function(xhr, settings) {
             function getCookie(name) {
                 var cookieValue = null;
                 if (document.cookie && document.cookie != '') {
                     var cookies = document.cookie.split(';');
                     for (var i = 0; i < cookies.length; i++) {
                         var cookie = jQuery.trim(cookies[i]);
                         // Does this cookie string begin with the name we want?
                     if (cookie.substring(0, name.length + 1) == (name + '=')) {
                         cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                         break;
                     }
                 }
             }
             return cookieValue;
             }
             if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                 // Only send the token to relative URLs i.e. locally.
                 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
             }
         } 
    });

    $('#modal_approve').on('shown.bs.modal', function () {
        $('#approve_note').focus();
    })
    
    $('#modal_refusal').on('shown.bs.modal', function () {
        $('#refusal_note').focus();
    })
    
    $(".workflow_log").click(function(event) {
        note = $(this).attr("hide")
        $('#workflow_log_note').html(note)
        $('#workflow_log').modal('show');
    });

    $("#btn_approve_ok").click(function(event) {
        $.ajax({
            type: "POST",
            url: "{% url ajax_technology_workflow_approve content_type_id object_id %}",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"yuankong",
            data: {note:$("#approve_note").val()},
            success: function(data) {
                $('#modal_approve').modal('hide');
                if (data.result == 0){
                    window.location.replace(window.location.pathname+window.location.search);
                }else{
                    $('#warning_note').html(data.msg);
                    $('#warning').modal('show');
                }
            },

            error: function () {
                alert('fail:'+data.result+' | '+data.msg);
            }
        });
    });

    $("#btn_refusal_ok").click(function(event) {
        $.ajax({
            type: "POST",
            url: "{% url ajax_technology_workflow_refusal content_type_id object_id %}",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"yuankong",
            data: {note:$("#refusal_note").val()},
            success: function(data) {
                $('#modal_refusal').modal('hide');
                if (data.result == 0){
                    window.location.replace(window.location.pathname+window.location.search);
                }else{
                    $('#warning_note').html(data.msg);
                    $('#warning').modal('show');
                }
            },

            error: function () {
                alert('fail:'+data.result+' | '+data.msg);
            }
        });
    });

</script>
{% endblock %}
